package com.ruoyi.consumer.utils;

import com.ruoyi.consumer.constance.TencentMapEnum;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class MapApiRequest {

    public static void main(String[] args) {
        try {
//            String apiKey = "GTMBZ-IUAYW-VEIRK-YEBQF-3ZPMO-N6B4X";  // Replace with your actual API key
            String address = "30.987889,103.646309";  // Replace with the address you want to query

            String apiUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location="
                    + URLEncoder.encode(address, "UTF-8")
                    + "&key=" + TencentMapEnum.api_key.getApiKey();

            String response = sendGetRequest(apiUrl);
            Object obj = JsonUtils.deserialize(response, Map.class);
            // Access the properties of addressComponent
            System.out.println("API Response:\n" + obj);
            System.out.println("address_component: " + ((Map) ((Map) obj).get("result")).get("address_component"));
            System.out.println("province: " + ((Map) ((Map) ((Map) obj).get("result")).get("address_component")).get("province"));
            System.out.println("city: " + ((Map) ((Map) ((Map) obj).get("result")).get("address_component")).get("city"));
            System.out.println("district: " + ((Map) ((Map) ((Map) obj).get("result")).get("address_component")).get("district"));
            System.out.println("street: " + ((Map) ((Map) ((Map) obj).get("result")).get("address_component")).get("street"));
            System.out.println("street_number: " + ((Map) ((Map) ((Map) obj).get("result")).get("address_component")).get("street_number"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 调用腾讯地图API获取省市区
     *
     * @param lng_lat
     * @return
     * @throws Exception
     */
    public static Object getAddress(String lng_lat) throws Exception {
        // Replace with your actual API key
        String address = lng_lat;  // Replace with the address you want to query

        String apiUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location="
                + URLEncoder.encode(address, "UTF-8")
                + "&key=" + TencentMapEnum.api_key.getApiKey();

        String response = sendGetRequest(apiUrl);
        Object obj = JsonUtils.deserialize(response, Map.class);
        return ((Map) ((Map) obj).get("result")).get("address_component");
    }

    //获取地图图片
    public static String getMapImg(String lng_lat) throws Exception {
        String address = lng_lat;  // Replace with the address you want to query

        String apiUrl = "https://apis.map.qq.com/ws/staticmap/v2/?"
                + "center="
                + URLEncoder.encode(address, "UTF-8")
                + "&zoom=17&size=700*525&maptype=roadmap&markers=icon:https://lbs.qq.com|"
                + URLEncoder.encode(address, "UTF-8")
                + "&key=" + TencentMapEnum.api_key.getApiKey();

        return apiUrl;
    }


    private static String sendGetRequest(String apiUrl) throws Exception {
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();
            return response.toString();
        } else {
            throw new RuntimeException("Failed to request the API. Response code: " + responseCode);
        }
    }
}
